package com.example.zmj.examplezmj.rabbitmqDemo.config;

import com.example.zmj.examplezmj.rabbitmqDemo.DirectReceiver;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

/**
 * Created by ZMJ on 2019/12/25.
 */
@Configuration
public class MessageListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;
    @Autowired
    DirectRabbitConfig directRabbitConfig;
    @Autowired
    private DirectReceiver directReceiver; // Direct消息接收处理类:消费者


    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(1); // 当前消费者
        container.setMaxConcurrentConsumers(1); // 最大并行消费者
        container.setPrefetchCount(1);// 消费与取消

        // 设置消费者的 ack 模式为手动确认模式,  RabbitMQ默认是自动确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);

        // 指定监听的队列
        container.setQueues(directRabbitConfig.TestDirectQueue());

        // 指定消费者
        container.setMessageListener(directReceiver);

        return container;
    }


}
